import React from 'react';
import { PageContainer } from '@ant-design/pro-components';
import { Card, Form, Input, Button, message, Space } from 'antd';
import { Solution } from './models/solutionModels';
import { createSolution } from '../../services/solutionService';
import { history } from 'umi';

const { TextArea } = Input;

const SolutionNew: React.FC = () => {
  const [form] = Form.useForm();
  const [loading, setLoading] = React.useState(false);

  const handleSubmit = async () => {
    try {
      const values = await form.validateFields();
      setLoading(true);
      
      const response = await createSolution(values);
      if (response.code === 200) {
        message.success('创建成功');
        history.push('/solutions');
      } else {
        message.error('创建失败');
      }
    } catch (error) {
      console.error('提交失败:', error);
      message.error('提交失败');
    } finally {
      setLoading(false);
    }
  };

  return (
    <PageContainer>
      <Card>
        <Form
          form={form}
          layout="vertical"
          onFinish={handleSubmit}
        >
          <Form.Item
            name="solutionName"
            label="方案名称"
            rules={[{ required: true, message: '请输入方案名称' }]}
          >
            <Input placeholder="请输入方案名称" />
          </Form.Item>

          <Form.Item
            name="userGroup"
            label="用户群"
            rules={[{ required: true, message: '请输入用户群' }]}
          >
            <Input placeholder="请输入用户群" />
          </Form.Item>

          <Form.Item
            name="description"
            label="描述"
            rules={[{ required: true, message: '请输入描述' }]}
          >
            <TextArea rows={4} placeholder="请输入描述" />
          </Form.Item>

          <Form.Item
            name="content"
            label="内容"
            rules={[{ required: true, message: '请输入内容' }]}
          >
            <TextArea rows={6} placeholder="请输入内容" />
          </Form.Item>

          <Form.Item
            name="documentationUrl"
            label="參考文档链接"
            rules={[
              { required: false, message: '请输入參考文档链接' }
            ]}
          >
            <Input placeholder="请输入參考文档链接" />
          </Form.Item>

          <Form.Item>
            <Space>
              <Button onClick={() => history.push('/solutions')}>
                取消
              </Button>
              <Button type="primary" htmlType="submit" loading={loading}>
                确定
              </Button>
            </Space>
          </Form.Item>
        </Form>
      </Card>
    </PageContainer>
  );
};

export default SolutionNew; 